﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { RequirementItemServiceProxy,RequirementItemDto,StandardPartsFeatureDto, UsableRangeDto,RequirementPackageDto ,RequirementItemExeInput,RoleUserServiceProxy} from '@shared/service-proxies/service-proxies';
import * as moment from 'moment';
import { StandardPartsDetailServiceProxy,DepartmentServiceProxy} from '@shared/service-proxies/service-proxies';
import {StandardPartsDetailSelf} from './standard-parts-detail_self';
@Component({
    selector: 'requirementDepartManagerItemEditComponent',
    templateUrl: './requirement-depart-manager-item_edit.component.html',
    styleUrls: ['./requirement-depart-manager-item_edit.component.less']

})
export class RequirementDepartManagerItemEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
    formData: RequirementItemDto = new RequirementItemDto();
    user: UserEditDto = new UserEditDto();

    passwordComplexityInfo = '';
    profilePicture: string;
    planDate:Date = new Date();
    rquirementPackageDto: RequirementPackageDto;
    // 标准件编码字段选择
    // 标准件
    standardPartsDetailIdAuto: StandardPartsDetailSelf = new StandardPartsDetailSelf()
    // 全部标准件
    standardPartsDetailAll:any[];
    // 帅选标准件
    standardPartsFiltered:StandardPartsDetailSelf[];

    unitOfUsings: Array<object>;;
    authorityUserOption: Array<object>;
    userName: string;
    filter(event):void{
        this.standardPartsFiltered = [];
        if(!this.standardPartsDetailAll){
            return ;
        }
        for(let i = 0; i < this.standardPartsDetailAll.length; i++) {
            let info:StandardPartsDetailSelf = new StandardPartsDetailSelf(this.standardPartsDetailAll[i]);

            let temp = event.query.toLowerCase();
            if(info.id.toLowerCase().indexOf(temp) >= 0 ) {
                this.standardPartsFiltered.push(info);
            }else if(info.features.displayName.toLowerCase().indexOf(temp) >= 0 || info.features.specification.toLowerCase().indexOf(temp) >= 0 || info.features.standardNumber.toLowerCase().indexOf(temp) >= 0 || info.features.materials.toLowerCase().indexOf(temp) >= 0){
                this.standardPartsFiltered.push(info);
            }
        }
    }
    rqeusetStandardPartsDetailAll(): void {
        this.primengTableHelper.showLoadingIndicator();
        this._standardPartsDetailServiceProxy.getAllDetail("", null, 1000, 0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.standardPartsDetailAll = result.items;
            });
    }
    
    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _departmentServiceProxy: DepartmentServiceProxy,
        private _requirementItemAppService: RequirementItemServiceProxy,
        private _standardPartsDetailServiceProxy: StandardPartsDetailServiceProxy,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.getunitOfUsings();
        this.getAuthorityUserOption();
    }
    getunitOfUsings() {
        this.unitOfUsings  = new Array<object>()
        this._departmentServiceProxy.getAllDpts()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.unitOfUsings = result;
            });
    }
    /**
     * 获取有权限的用户
     */
    getAuthorityUserOption() {
        this.authorityUserOption = new Array<object>()
        // 车间审批领导
        this._roleUserServiceProxy.getUsersByRole(0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.authorityUserOption = result;
            });
    }
    show(): void {
        this.formData = new RequirementItemDto();
        this.formData.features = new StandardPartsFeatureDto();
        this.formData.usableRange = new UsableRangeDto();
        this.standardPartsDetailIdAuto = new StandardPartsDetailSelf()
        this.planDate = new Date();
        this.active = true;
        this.modal.show();
        this.rqeusetStandardPartsDetailAll();
    }

    onShown(): void {
        //document.getElementById('code').focus();
    }
    /**
     * 需求详细添加保存
     * 需求详细编辑保存
     */
    save(): void {
        this.saving = true;
        this.formData.features = this.standardPartsDetailIdAuto.features;
        this.formData.standardPartsDetailId = this.standardPartsDetailIdAuto.id;
        // 日期转换
        this.formData.requiredTimeOfModel = moment(this.planDate)
        this._requirementItemAppService.createRequirementItem(this.formData)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.examineRequirementItem(result);
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    /**
     * 需求提交审核
     * @param record 
     */
    examineRequirementItem(id: number) {
        let params: RequirementItemExeInput = new RequirementItemExeInput();
        params.id = id;
        params.userId = this.userName;
        //params.userId = 
        this._requirementItemAppService.examineRequirementItem(params)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.hideLoadingIndicator();
                //this.getRequirementPackages();
            });
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
